package com.smartguard.interceptor;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.smartguard.common.Const;
import com.smartguard.util.JSONUtil;

public class AuthorityInterceptor implements Interceptor {

	@Override
	public void destroy() {
		//信息
		System.out.println("权限过滤器destroying...");
	}

	@Override
	public void init() {
		//输出
		System.out.println("权限过滤器initing...");
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		//取得session
		Map<String, Object> session = invocation.getInvocationContext().getSession();
		Object obj = session.get(Const.SESSION_USER);
		
		//判断是否登录
		if(obj != null){
			System.out.println("用户验证成功");
			return invocation.invoke();
		}
		else{
			//如果没有登录则跳转到输出json的result
			System.out.println("用户验证失败");
			JSONObject json = JSONUtil.newErrMsgJSONObject("未登录");
			invocation.getInvocationContext().put("reason", json);
			return "nologin";
		}
	}

}
